<template>
    <div>
        <div class="e-form">
            <BillTop @cancel="handleClose"></BillTop>
            <div class="tabs" style="margin-bottom:120px">
                <div class="tabs-title">基本信息</div>
                <div style="width: 100%" class="form">
                    <el-form :model="formData" :label-width="formLabelWidth" :rules="rulesBase" ref="rulesBase" class="demo-ruleForm">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="合同编号：">
                                    <span>{{formData.contractNumber}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="所属机构：">
                                    <span>{{ formData.orgName }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="合同名称：">
                                    <span>{{formData.contractName}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="合同类型：">
                                    <span>设备租赁收入合同</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="不含税金额(元)：">
                                    <span>{{
                                        formData.taxExcludedAmount
                                    }}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="合同金额(元)：">
                                    <span>{{
                                        formData.contractAmount
                                    }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                           <el-col :span="12">
                                <el-form-item label="终止日期：" prop="stopDate">
                                    <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择终止日期" v-model="formData.stopDate"></el-date-picker>
                                </el-form-item>
                            </el-col>
                             <el-col :span="12">
                                <el-form-item label="已结算金额：">
                                   <span>{{formData.serverUnitAmount}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="终止原因：" prop="stopReason">
                                    <el-input type="textarea" v-model="formData.stopReason" autocomplete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                         <el-row>
                            <el-col :span="12">
                                <el-form-item label="经办人：">
                                    <span>{{userInfo.userName}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="录入日期：">
                                    <span>{{formData.gmtCreate}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="状态：">
                                    <span>{{state.find(x=>x.value === formData.state).label}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </div>
            <div class="buttons">
                <el-button type="primary" size="small" style="background: #2e61d7" @click="addSave"
                    >保存</el-button
                >
                <el-button size="small" @click="handleClose">取消</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { formatDate } from '@/utils/common.js'
import equimentLeaseIncomeApply from '@/api/balanceContractApi/equimentLeaseIncomeApi/equimentLeaseIncomeApply.js'
import equimentLeaseIncomeEnd from '@/api/balanceContractApi/equimentLeaseIncomeApi/equimentLeaseIncomeEnd.js'
import { hideLoading, showLoading } from '@/utils/common'
export default {
    data () {
        return {
            //基础信息
            formData: {
                'approvedTime': '',
                'billId': '',
                'contractAmount': 0,
                'contractName': '',
                'contractNumber': '',
                'contractTypeName': '',
                'founderId': '',
                'founderName': '',
                'gmtCreate': '',
                'gmtModified': '',
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDescription': '',
                'nullifyReason': '',
                'orgId': '',
                'orgName': '',
                'serverUnitAmount': 0,
                'sourceId': '',
                'sourceState': 0,
                'state': 0,
                'stopDate': '',
                'stopReason': '',
                'taxExcludedAmount': 0,
                'workId': ''
            },
            saveFormData: {
                'billId': '',
                'sourceId': '',
                'stopDate': '',
                'stopReason': ''
            },
            formLabelWidth: '180px',
            billid: this.$route.query.billid,
            rulesBase: {
                stopReason: [
                    { required: true, message: '请输入终止原因', trigger: 'blur' },
                    { min: 1, max: 500, message: '最多输入500字', trigger: 'blur' }
                ],
                stopDate: [
                    { required: true, message: '请选择终止日期', trigger: 'change' },
                    { validator: (rule, value, callback)=>{
                        const singTime = new Date(this.formData.stopDate).getTime() //终止日期
                        const startTime = new Date(formatDate(new Date(), 'yyyy-MM-dd')).getTime() //当前时间
                        if(singTime > startTime) {
                            callback(new Error('终止日期不能大于当前日期'))
                        }else{
                            callback()
                        }

                    }, trigger: 'change' }
                ],
            }
        }
    },
    created () {
        this.getBaseInfo()
    },
    mounted () {
    },
    components: {
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify,
            userInfo: state => state.userInfo,
            state: state => state.equip.equipData.state, //通用流程状态
        })
    },
    methods: {
        //获取当前日期
        getCurrentTime () {
            this.formData.stopDate = formatDate(new Date(), 'yyyy-MM-dd')
        },
        //默认获取基本信息
        getBaseInfo () {
            showLoading()
            equimentLeaseIncomeApply.getInfo(this.billid).then(res=>{
                for(let key in this.formData) {
                    this.formData[key] = res[key]
                }
                this.formData.sourceId = this.billid
                this.formData.billId = ''
                this.getCurrentTime()
                hideLoading()
            })
        },
        //基本信息保存
        addSave () {
            this.$refs.rulesBase.validate(valid => {
                if (valid) {
                    showLoading()
                    this.formData.sourceState = this.$route.query.sourceState
                    equimentLeaseIncomeEnd.createInfo(this.formData).then(res=>{
                        hideLoading()
                        this.clientPop('info', '保存成功，是否继续？', ()=>{
                            this.$router.replace({
                                path: '/equimentLeaseIncomeEndEdit',
                                query: {
                                    billid: res,
                                    name: 'attach'
                                }
                            })
                        }, null, this.handleClose)
                    })
                } else {
                    return false
                }
            })
        },
        //取消
        handleClose () {
            hideLoading()
            this.$router.replace('/equimentLeaseIncomeContract')
        },
    },
}

</script>

<style lang='scss' scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
.e-table{
    min-height: auto;
    background: #fff;
}
.separ{
   width: 30px;
   height: 40px;
   line-height: 18px;
   text-align: center;
}
::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}

</style>